package com.imau.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.imau.bean.Role;
import com.imau.service.RoleService;
import com.imau.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RoleController", value = "/role/*")
public class RoleController extends Baseconterllor {


    private RoleService roleService = new RoleServiceImpl();


    /**
     * 添加教师
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        Role role = JSON.parseObject(jsonString, Role.class);

        //3.保存
        Role entiry = roleService.save(role);

        //4.返回响应

        this.writeResult(response, entiry == null ? false : true);


    }


    /**
     * 删除
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void removeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String id = request.getParameter("id");


        //删除
        int row = roleService.deleteById(id);
        //4.返回响应

        this.writeResult(response, row > 0 ? true : false);


    }


    protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        Role role = JSON.parseObject(jsonString, Role.class);

        //3.保存
        int row = roleService.updateRole(role);

        //4.返回响应

        this.writeResult(response, row > 0 ? true : false);


    }


    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        Role role = JSON.parseObject(jsonString, Role.class);

        String currentPage = request.getParameter("currentPage");

        String pageSize = request.getParameter("pageSize");

        //3.保存

        PageInfo<Role> pageInfo = roleService.queryListByPagte(role, Integer.valueOf(currentPage), Integer.valueOf(pageSize));

        //4.返回响应
        writeResultAndData(response, pageInfo);


    }


    protected void getInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String id = request.getParameter("id");


        Role role = roleService.findById(id);

        //4.返回响应
        writeResultAndData(response, role);


    }


    protected void assignPermission(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //角色Id

        String id = request.getParameter("id");

        String s = request.getReader().readLine();

        String[] perids = JSON.parseObject(s, String[].class);

        int row = roleService.assignPermission(id, perids);


        //4.返回响应
        this.writeResult(response, row > 0 ? true : false);


    }
}
